import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)


// 用到路由懒加载， 提高渲染速度
const routes = [
  {
    path: '/workerList',
    name: 'workerList',
    component: () => import('../views/workerList')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login')
  },
  {
    path: '/edit/:id',
    name: 'edit',
    component: () => import('../views/edit')
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: () => import('../views/workerDetail')
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '*',
    component: () => import('../components/404')
  }
 
]

const router = new VueRouter({
  routes
})

// router.beforeEach((to,from,next)=>{
//   const hasLogin = localStorage.getItem('loginState')
//   if (hasLogin){
//       next()
//   }else{
//     next('/login')
//   }
  
// })
router.beforeEach((to,from,next) => {
  const token = localStorage.getItem("loginState");
  if(!token && to.path != '/login'){//未登录，强制登录
      next({
          name:"login"  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      });
  }else{
      next();
  }
});

// "@vue/standard"
 
export default router
